﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Ivory.GUI
{
    public sealed class GUI : Dictionary<string, GUIComponent>
    {
        //Fields

        //Construct
        public GUI()
            : base()
        {

        }

        //Methods
        public void AddComponent(GUIComponent component)
        {
            if (component.ID == null)
                component.ID = string.Format("component{0}", this.Count);

            if (this.ContainsKey(component.ID))
                throw new IvoryFrameworkError(Errors.ComponentNameNotUnique(component.ID));

            this.Add(component.ID, component);
        }
        
        public GUIComponent Get(string id)
        {
            try
            {
                return this[id];
            }
            catch (KeyNotFoundException)
            {
                throw new IvoryFrameworkError(Errors.ComponentNotFound(id));
            }
        }
        public T Get<T>(string id)
        {
            object c = Get(id);
            return (T)c; // as T;
        }

        public void DrawGUI(SpriteBatch batch, GameTime time)
        {
            foreach (GUIComponent component in this.Values)
            {
                if(component.IsVisible)
                    component.DrawComponent(batch, time);
            }
        }
        public void UpdateGUI(InputControl input, GameTime time)
        {
            foreach (GUIComponent component in this.Values)
            {
                if (component.IsVisible)
                    component.UpdateComponent(input, time);
            }
        }

        //public void LoadGUI()
        //{
        //    foreach (GUIComponent component in this.Values)
        //    {
        //        component.LoadComponent();
        //    }
        //}
    }
}
